package com.fit.pages.scientificField;

import java.util.List;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.Inject;


import com.fit.entities.ScientificField;
import com.fit.pages.Confirmation;

import com.fit.dao.scientificField.ScientificFieldDAO;

public class AddScientificField {

	
	@Property
	private List <ScientificField> scientificFields;
	
	@Property
	private ScientificField scientificField;
	
	@Persist
	@Property
	private String msg;
	
	@Inject
	private ScientificFieldDAO scientificFieldDAO;
	
	@InjectPage
	private Confirmation confirmation;
	
	@InjectPage
	private AddScientificField addScientificField;
	
	void onActivate(){
		scientificFields = scientificFieldDAO.findAll();
	}
	
	
	@CommitAfter
	Object onSuccess() {
		confirmation.setError("");
		confirmation.setConfirmation("");
		ScientificField temp = scientificFieldDAO.findByID(scientificField.getId());
		
		if (temp == null) {
			if (scientificFieldDAO.save(scientificField)) {
				//confirmation.setConfirmation(u"Language successfully inserted!");
				msg="ScientificField successfully inserted!";
				return addScientificField;
			}
			confirmation.setConfirmation("Language is null!");
			return confirmation;
		}

		confirmation.setError("Language with " + scientificField.getId()
				+ " ID already exists!");

		return confirmation;
	}
	
	@CommitAfter
	@OnEvent(component = "deleteLink")
	Object onDeleteLink(Long id) {
		try {
			scientificFieldDAO.deleteByID(id);
			msg="ScientificField successfully deleted!";
			return null;
		} catch (Exception e) {
			confirmation.setConfirmation("Greska prilikom brisanja zapisa korisnika! ");			
			return confirmation;
		}
	}
	
}
